<?php

/**
 * 存储/推送数据至WMS仓库
 * @auther QiuXiangCheng
 * @date 2016/08/10
 */
class Interface_WmsController extends Controller_Interface {

	const STRUCTURE_CHANGE = [
		'sku' => 'goods_sn',
		'stock_code' => 'warehouse_code',
		'stock_available' => 'stock_can_allot',
		'virtual_stock' => 'virtual_stock',
		'stock_process' => 'stock_on_way',
		'stock_breakdown' => 'rejects_stock',
		'stock_post' => 'stock_wait_for_send',
		'update_time' => 'update_time',
		//'update_time' => 'last_storage_time',
		'locked_available' => 'frozen_stock',
	];

	/**
	 * 接收信息时签名认证
	 * @var (string)
	 */
	public $sign;

	public $signKey;

	public function init() {
		parent::init();
		$this->stockModel = new Base_Model_Stock;
		$this->signKey = Helper_Config::get('base', 'crypt', 'key');
	}

	/**
	 * 处理深圳仓库存变更通知
	 * @return [type] [description]
	 */
	public function stockChangedAction() {
		$title = '深圳仓库存变更通知：';
		$postData = $this->getRequest()->getPost();

		if (!isset($postData['key'])) {
			$return['status'] = 'fail';
			$this->log->record($title . '签名缺失！', Log::ALERT);
			$this->_helper->json([$return]);
		}

		//有些平台把通知数据放在data字段，有些放json字段
		if (!isset($postData['data'])) {
			$return['status'] = 'fail';
			$this->log->record($title . '数据为空！', Log::ALERT);
			$this->_helper->json([$return]);
		}
		$stockData = $postData['data'];

		if (!$this->verifySign($stockData, $postData['key'])) {
			$return['status'] = 'fail';
			$this->log->record($title . '签名错误！', Log::ALERT);
			$this->_helper->json([$return]);
		}

		$stockList = json_decode(urldecode($stockData), true);
		if (!is_array($stockList)) {
			$return['status'] = 'fail';
			$this->log->record($title . '通知数据格式异常！' . print_r($stockData, true), Log::ALERT);
			$this->_helper->json([$return]);
		}

		$writeDb = Model_Db::getInstance('write_db');
		$allReturn = [];
		foreach ($stockList as $stock) {
			$_stockData = [];
			foreach (self::STRUCTURE_CHANGE as $fromFields => $toFields) {
				if (isset($stock[$fromFields])) {
					$_stockData[$toFields] = $stock[$fromFields];
				}
			}
			$return['sku'] = $stock['sku'];
			$return['id'] = $stock['id'];
			try {
				Model_Base::insertOrUpdate($writeDb, 'p_product_warehouse_stock', $_stockData);
				$return['status'] = 'succeed';
			} catch (Exception $e) {
				$return['status'] = 'fail';
				$this->log->record($title . '数据更新失败,原因:' . $e->getMessage(), Log::ERR);
			}

			$allReturn[] = $return;
		}
		$this->log->record($title . '处理成功！');
		$this->_helper->json($allReturn);
	}

	/**
	 * 处理商品海淘仓库存变更通知
	 * @return [type] [description]
	 */
	public function taoStockChangedAction() {
		$this->_overseas('海淘仓');
	}

	/**
	 * 处理商品海外仓库存变更通知
	 * @return [type] [description]
	 */
	public function overseasStockChangedAction() {
		$this->_overseas('海外仓');
	}

	protected function _overseas($source) {
		$postData = $this->getRequest()->getPost();
		$title = $source . '库存变更通知：';

		if (!isset($postData['key'])) {
			$return['stockStr'] = '签名缺失！';
			$return['stockStatus'] = 'fail';
			$this->log->record($title . '签名缺失！', Log::ALERT);
			$this->_helper->json([$return]);
		}

		//有些平台把通知数据放在data字段，有些放json字段
		if (!isset($postData['data']) && !isset($postData['json'])) {
			$return['stockStr'] = '数据为空！';
			$return['stockStatus'] = 'fail';
			$this->log->record($title . '数据为空！', Log::ALERT);
			$this->_helper->json([$return]);
		}
		$stockData = isset($postData['data']) ? $postData['data'] : $postData['json'];
		if (!$this->verifySign($stockData, $postData['key'])) {
			$return['stockStr'] = '签名错误！';
			$return['stockStatus'] = 'fail';
			$this->log->record($title . '签名错误！', Log::ALERT);
			$this->_helper->json([$return]);
		}

		$stockList = json_decode(urldecode($stockData), true);

		if (!is_array($stockList)) {
			$return['stockStr'] = '通知数据格式异常！';
			$return['stockStatus'] = 'fail';
			$this->log->record($title . '通知数据格式异常！' . print_r($stockData, true), Log::ALERT);
			$this->_helper->json([$return]);
		}

		$writeDb = Model_Db::getInstance('write_db');

		$allReturn = [];
		foreach ($stockList as $stock) {
			$_stockData = [];
			foreach (self::STRUCTURE_CHANGE as $fromFields => $toFields) {
				if (isset($stock[$fromFields])) {
					$_stockData[$toFields] = $stock[$fromFields];
				}
			}
			$return['sku'] = $stock['sku'];
			$return['id'] = $stock['id'];

			try {
				Model_Base::insertOrUpdate($writeDb, 'p_product_warehouse_stock', $_stockData);
				$return['stockStr'] = '成功！';
				$return['stockStatus'] = 'succeed';
				$return['shelfStr'] = '成功！';
				$return['shelfStatus'] = 'succeed';
			} catch (Exception $e) {
				$return['stockStr'] = '数据更新失败' . $e->getMessage();
				$return['stockStatus'] = 'fail';
				$this->log->record($title . '数据更新失败,原因:' . $e->getMessage(), Log::ERR);
			}

			$allReturn[] = $return;
		}
		$this->log->record($title . '处理成功！');
		$this->_helper->json($allReturn);
	}

	/**
	 * 处理需求调拨出库通知
	 * @return [type] [description]
	 * @author shipanpan
	 */
	public function transferOutWarehouseAction() {
		$applyId = '';
		$goodsSn = '';
		$outQuantity = '';

		$stockTransferApplyModel = new Interface_Model_StockTransferApply();

		$stockTransferApplyModel->transferOutWarehouse($applyId, $goodsSn, $outQuantity);
	}
}
